package com.xtoneict;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.ScriptableObject;

import java.io.FileReader;

public class RhinoExample {

    public static void main(String[] args) throws Exception{
        // 创建执行上下文
        Context context = Context.enter();
        context.setOptimizationLevel(-1);
        try {
            // 创建全局作用域
            ScriptableObject scope = context.initStandardObjects();

            // 编译JavaScript代码
            Script script = context.compileReader(new FileReader("C:\\Users\\62772\\Desktop\\test1.js"), "script", 1, null);
            //Script script1 = context.compileReader(new FileReader("C:\\Users\\62772\\Desktop\\test.js"), "script1", 1, null);

            // 执行JavaScript代码
            Object result = script.exec(context, scope);
            //Object result1 = script1.exec(context, scope);

            // 调用JavaScript方法
            Object result1 = context.evaluateString(scope, "window.CaData_KXC('5', '3');", "<cmd>", 1, null);

            // 输出结果
            System.out.println("JavaScript执行结果：" + result);
            System.out.println("JavaScript执行结果：" + result1);
            //System.out.println("调用JavaScript方法结果：" + addResult);
        } finally {
            // 退出执行上下文
            Context.exit();
        }
    }
}